<template>
  <div class="new_auction_Bidding_detail">
    <c-title :hide="false" :text="pageTitle"></c-title>
    <div v-for="(item, ind) in list" :key="ind" @click="tapActive(item)">
      <bidding :infoData="item" @pickUp="pickUpChild" @cancelAuction="cancelAuction" @settle_handle="settle_handle"></bidding>
    </div>
    <option_popup v-if="selectItem" :isPick="true" :showPopup="show" :item="selectItem" @pickUp="pickUpChild_popup" @closePopup="closePopup"></option_popup>
  </div>
</template>
<script>
import bidding from "./components/bidding.vue";
import option_popup from "./components/optionPopup.vue";
import { scrollMixin } from "utils/mixin";
import { Dialog } from "vant";
export default {
  mixins: [scrollMixin],
  data() {
    return {
      show: false,
      pageTitle: "",
      list: [],
      selectItem: null,

      isLoadMore: true,
      page: 1,
      total_page: 0
    };
  },
  components: { bidding, option_popup },
  activated() {
    this.pageTitle = this.$route.meta.title;
    this.init();
    this.getData();
  },
  methods: {
    settle_handle({ id, data }) {
      Dialog.confirm({
        message: `是否确认结算${data.has_one_goods.title}*${data.settle_count}个`
      })
        .then(() => {
          $http.post("plugin.new-auction.frontend.auction.settle", { id }, "").then(({ result, msg }) => {
            this.$toast(msg);
            this.init();
            this.getData();
          });
        })
        .catch(() => {
          // on cancel
        });
    },
    tapActive(item) {
      if (this.$route.name == "new_auction_Bidding_detail") {
        this.$router.push(this.fun.getUrl("new_auction_detail", { id: item.activity_id }));
      }
    },
    init() {
      this.page = 1;
      this.total_page = 0;
      this.isLoadMore = true;
      this.list = [];
    },
    async pickUpChild_popup(e) {
      let json = {
        new_auction_id: e.id,
        new_auction_pick_num: e.value
      };
      this.$router.push(this.fun.getUrl("new_auction_order", json, { tag: "new_auction" }));
      this.show = false;
      this.selectItem = null;
      this.init();
      this.getData();
    },
    async pickUpChild(e) {
      if (this.$route.name == "new_auction_Auction_details") {
        return;
      }
      this.show = true;
      this.selectItem = e.item;
    },
    // 取消参拍
    async cancelAuction(e) {
      let json = {
        id: e.id
      };
      let { msg, result } = await $http.post("plugin.new-auction.frontend.auction.cancelAuction", json, ".");
      if (result) {
        this.$toast(msg);
        this.init();
        this.getData();
      } else {
        this.$toast(msg);
      }
    },
    closePopup() {
      this.show = false;
    },
    async getData() {
      let urlstr = "plugin.new-auction.frontend.auction";
      if (this.$route.name == "new_auction_Auction_details") {
        urlstr = "plugin.new-auction.frontend.entrust";
      }
      let { data, msg, result } = await $http.post(urlstr, {}, "");
      if (result) {
        this.list = data.data;
        this.isLoadMore = true;
        this.total_page = data.last_page;
      } else {
        this.isLoadMore = false;
        this.$toast(msg);
      }
    },
    async getMoreData() {
      if (!this.isLoadMore) return;
      this.isLoadMore = false;
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page += 1;
        let json = {
          page: this.page
        };
        let urlstr = "plugin.new-auction.frontend.auction";
        if (this.$route.name == "new_auction_Auction_details") {
          urlstr = "plugin.new-auction.frontend.entrust";
        }
        let { data, msg, result } = await $http.post(urlstr, json, "");
        this.isLoadMore = true;
        if (result) {
          this.list = this.list.concat(data.data);
        } else {
          this.page = this.page - 1;
          this.isLoadMore = false;
          this.$toast(msg);
        }
      }
    }
  }
};
</script>
<style lang="scss" scoped></style>
